package com.sui.bigdata.rtcadmin.util;

import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author YongChen
 * @date 2019/12/4 15:44
 * @description
 * @email yong_chen@sui.com
 */
public class ScanUdfUtils {

    final static String UDF_PACKAGE = "com/sui/bigdata/flink/function";
    final static String SCALAR = "scalar";
    final static String TABLE = "table";
    final static String AGGREGATE = "aggregate";

    public static String getUdfs(String udfDirPath) throws Exception {
        StringBuilder udfs = new StringBuilder();
        File files = new File(udfDirPath);
        for (File file : files.listFiles()) {
            if (file.getAbsolutePath().endsWith("jar")){
            JarFile jarFile = new JarFile(file.getAbsolutePath());
            Enumeration<JarEntry> entrys = jarFile.entries();
            while (entrys.hasMoreElements()) {
                JarEntry jarEntry = entrys.nextElement();
                String className = jarEntry.getName();
                if (className.contains(UDF_PACKAGE) && className.contains("class")) {
                    className = className.replace(".class", "");
                    String funName = className.split("/")[className.split("/").length - 1];
                    char[] chars = funName.toCharArray();
                    chars[0] += 32;
                    className = className.replace("/", ".");

                    if (className.contains(SCALAR) || className.contains(TABLE) || className.contains(AGGREGATE)) {
                        udfs.append("create function IF NOT EXISTS " + String.valueOf(chars) + " as '" + className + "';");
                    }

                }
            }

        }
        }
       return udfs.toString();
    }

    public static void main(String[] str) throws Exception {

        String sql = getUdfs("D:\\htw\\software\\env\\udf");
        System.out.println(sql);
        String msg ="{\"executeTime\":1612417907000,\"partition\":2,\"rowData\":\"{\\\"putout_date\\\":\\\"\\\",\\\"fund_type\\\":\\\"1\\\",\\\"product_id\\\":\\\"2009231002736504708034560\\\",\\\"remark\\\":\\\"\\\",\\\"apply_id\\\":\\\"BA2012061029669580253286401\\\",\\\"suid\\\":\\\"u_33klq5d\\\",\\\"create_date\\\":\\\"2021-02-04 13:51:48\\\",\\\"bond_rate\\\":\\\"0.0\\\",\\\"business_sum\\\":\\\"1200.0\\\",\\\"fund_order_no\\\":\\\"\\\",\\\"interest_rate\\\":\\\"34.0\\\",\\\"credit_grade\\\":\\\"A\\\",\\\"term_unit\\\":\\\"monthly\\\",\\\"pay_amount\\\":\\\"1200.0\\\",\\\"loan_days\\\":\\\"null\\\",\\\"del_flag\\\":\\\"0\\\",\\\"gps_info\\\":\\\"\\\",\\\"fee_term_id\\\":\\\"PEN2009231002741625949839360\\\",\\\"putout_rate\\\":\\\"34.0\\\",\\\"asset_audit_pass_time\\\":\\\"\\\",\\\"service_rate\\\":\\\"0.0\\\",\\\"business_type\\\":\\\"4\\\",\\\"rate_term_id\\\":\\\"RATE2009231002741509606776832\\\",\\\"contract_no\\\":\\\"SSFD20210204135127737706005\\\",\\\"create_by\\\":\\\"admin\\\",\\\"device_type\\\":\\\"android\\\",\\\"institutions_finance_id\\\":\\\"ZJ2009231002738723768295424\\\",\\\"institutions_traffic_id\\\":\\\"\\\",\\\"occur_type\\\":\\\"\\\",\\\"institutions_insurance_id\\\":\\\"\\\",\\\"fin_term_id\\\":\\\"FIN2009231002741561637597184\\\",\\\"rpt_term_id\\\":\\\"RPT2019031812526435111546880\\\",\\\"annual_rate\\\":\\\"34.0\\\",\\\"purpose\\\":\\\"EDU\\\",\\\"institutions_guarantee_id\\\":\\\"DB2009231002739121100271616\\\",\\\"sequence_id\\\":\\\"\\\",\\\"update_by\\\":\\\"admin\\\",\\\"currency\\\":\\\"CNY\\\",\\\"id\\\":\\\"BP2102041051361712917606400\\\",\\\"institutions_asset_id\\\":\\\"ZC2009231002738787572047872\\\",\\\"tongdun_event_id\\\":\\\"\\\",\\\"globalorder\\\":119019483149760203,\\\"refuse_reason\\\":\\\"\\\",\\\"putout_status\\\":\\\"0\\\",\\\"proc_ins_id\\\":\\\"\\\",\\\"bc_id\\\":\\\"BC190726848713467539296256\\\",\\\"period_id\\\":\\\"2009231002743265803804672\\\",\\\"executetime\\\":1612417907000,\\\"maturity_date\\\":\\\"\\\",\\\"term\\\":\\\"3\\\",\\\"clear_term_id\\\":\\\"\\\",\\\"update_date\\\":\\\"2021-02-04 13:51:48\\\",\\\"consult_rate\\\":\\\"0.0\\\",\\\"customer_id\\\":\\\"LCU190726848713467509936128\\\"}\",\"schemaName\":\"finance_suigo\",\"tableName\":\"business_putout\",\"topic\":\"rtsync.bdl_suigo_business_putout\",\"type\":\"INSERT\"}";
        JSONObject jsonObject = JSONObject.parseObject(msg);
        System.out.println(jsonObject);
    }

}
